package cn.hx.controller.system;

import cn.hx.entity.ItemGroup;
import cn.hx.service.ItemGroupService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.util.UUID;

@Controller
public class ItemGroupController {
    @Resource
    private ItemGroupService itemGroupService;


    @GetMapping("system/item/item_group")
    private String groupList(ItemGroup itemGroup,
                             ModelMap modelMap,
                             @RequestParam(defaultValue = "1") int page,
                             @RequestParam(defaultValue = "10") int size){
        modelMap.addAttribute("group",itemGroupService.page(itemGroup,page, size));
        return "system/item/item_group";
    }
    @GetMapping("system/item/item_material/group_change/{id}")
    private String groupChang(@PathVariable Integer id,ModelMap map){
        map.addAttribute("list",itemGroupService.queryById(id));
        return "system/item/item_group_update";
    }
    @Value("${web.upload-path}")
    private String filePath;
    @PostMapping("system/item/item_material/group_change/update/{id}")
    private String update(ItemGroup itemGroup, MultipartFile file) throws IOException {
        if (file != null && !file.isEmpty()) {
            String filename = file.getOriginalFilename();
            String type = filename.substring(filename.lastIndexOf("."));
            String uuid = UUID.randomUUID().toString().replaceAll("-", "");
//            file.transferTo(new File("D:\\file\\" + uuid + type));
//            itemGroup.setIcon("/img/item_group_icon/" + uuid + type);
            File file1 = new File(filePath + "/" + type);
            if (!file1.isDirectory()){
                file1.mkdirs();
            }
            file.transferTo(new File(filePath+"/" +type+"/"+ uuid + type));
            itemGroup.setIcon("/"+type+"/" + uuid + type);
        }
        itemGroupService.update(itemGroup);
        return "redirect:/system/item/item_group";
    }
    @GetMapping("system/item/item_material/del/{id}")
    private String delete(@PathVariable Integer id){
        itemGroupService.deleteById(id);
        return "redirect:/system/item/item_group";
    }
    @GetMapping("system/item/item_group/insert")
    private String insert(){
        return "system/item/item_group_save";
    }


    @PostMapping("system/item/item_group/save")
    private String save(ItemGroup itemGroup, MultipartFile file) throws IOException {
        if (file != null && !file.isEmpty()) {

            String filename = file.getOriginalFilename();
            String type = filename.substring(filename.lastIndexOf("."));
            String uuid = UUID.randomUUID().toString().replaceAll("-", "");
//            file.transferTo(new File("D:\\file\\" + uuid + type));
//            itemGroup.setIcon(uuid + type);
            File file1 = new File(filePath + "/" + type);
            if (!file1.isDirectory()){
                file1.mkdirs();
            }
            file.transferTo(new File(filePath+"/" +type+"/"+ uuid + type));
            itemGroup.setIcon("/"+type+"/" + uuid + type);
        }
        itemGroupService.insert(itemGroup);
        return "redirect:/system/item/item_group";
    }
}
